home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_set.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  61KB  |  1,832 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import unittest
  5. from test import test_support
  6. from weakref import proxy
  7. import operator
  8. import copy
  9. import pickle
  10. import os
  11.  
  12. class PassThru(Exception):
  13.     pass
  14.  
  15.  
  16. def check_pass_thru():
  17.     raise PassThru
  18.     yield 1
  19.  
  20.  
  21. class TestJointOps(unittest.TestCase):
  22.     
  23.     def setUp(self):
  24.         self.word = word = 'simsalabim'
  25.         self.otherword = 'madagascar'
  26.         self.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  27.         self.s = self.thetype(word)
  28.         self.d = dict.fromkeys(word)
  29.  
  30.     
  31.     def test_new_or_init(self):
  32.         self.assertRaises(TypeError, self.thetype, [], 2)
  33.  
  34.     
  35.     def test_uniquification(self):
  36.         actual = sorted(self.s)
  37.         expected = sorted(self.d)
  38.         self.assertEqual(actual, expected)
  39.         self.assertRaises(PassThru, self.thetype, check_pass_thru())
  40.         self.assertRaises(TypeError, self.thetype, [
  41.             []])
  42.  
  43.     
  44.     def test_len(self):
  45.         self.assertEqual(len(self.s), len(self.d))
  46.  
  47.     
  48.     def test_contains(self):
  49.         for c in self.letters:
  50.             self.assertEqual(c in self.s, c in self.d)
  51.         
  52.         self.assertRaises(TypeError, self.s.__contains__, [
  53.             []])
  54.         s = self.thetype([
  55.             frozenset(self.letters)])
  56.         self.assert_(self.thetype(self.letters) in s)
  57.  
  58.     
  59.     def test_union(self):
  60.         u = self.s.union(self.otherword)
  61.         for c in self.letters:
  62.             if not c in self.d:
  63.                 pass
  64.             self.assertEqual(c in u, c in self.otherword)
  65.         
  66.         self.assertEqual(self.s, self.thetype(self.word))
  67.         self.assertEqual(type(u), self.thetype)
  68.         self.assertRaises(PassThru, self.s.union, check_pass_thru())
  69.         self.assertRaises(TypeError, self.s.union, [
  70.             []])
  71.         for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  72.             self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd'))
  73.             self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
  74.             self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
  75.             self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef'))
  76.         
  77.  
  78.     
  79.     def test_or(self):
  80.         i = self.s.union(self.otherword)
  81.         self.assertEqual(self.s | set(self.otherword), i)
  82.         self.assertEqual(self.s | frozenset(self.otherword), i)
  83.         
  84.         try:
  85.             self.s | self.otherword
  86.         except TypeError:
  87.             pass
  88.  
  89.         self.fail('s|t did not screen-out general iterables')
  90.  
  91.     
  92.     def test_intersection(self):
  93.         i = self.s.intersection(self.otherword)
  94.         for c in self.letters:
  95.             if c in self.d:
  96.                 pass
  97.             self.assertEqual(c in i, c in self.otherword)
  98.         
  99.         self.assertEqual(self.s, self.thetype(self.word))
  100.         self.assertEqual(type(i), self.thetype)
  101.         self.assertRaises(PassThru, self.s.intersection, check_pass_thru())
  102.         for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  103.             self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc'))
  104.             self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
  105.             self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
  106.             self.assertEqual(self.thetype('abcba').intersection(C('ef')), set(''))
  107.         
  108.  
  109.     
  110.     def test_and(self):
  111.         i = self.s.intersection(self.otherword)
  112.         self.assertEqual(self.s & set(self.otherword), i)
  113.         self.assertEqual(self.s & frozenset(self.otherword), i)
  114.         
  115.         try:
  116.             self.s & self.otherword
  117.         except TypeError:
  118.             pass
  119.  
  120.         self.fail('s&t did not screen-out general iterables')
  121.  
  122.     
  123.     def test_difference(self):
  124.         i = self.s.difference(self.otherword)
  125.         for c in self.letters:
  126.             if c in self.d:
  127.                 pass
  128.             self.assertEqual(c in i, c not in self.otherword)
  129.         
  130.         self.assertEqual(self.s, self.thetype(self.word))
  131.         self.assertEqual(type(i), self.thetype)
  132.         self.assertRaises(PassThru, self.s.difference, check_pass_thru())
  133.         self.assertRaises(TypeError, self.s.difference, [
  134.             []])
  135.         for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  136.             self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab'))
  137.             self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc'))
  138.             self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a'))
  139.             self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc'))
  140.         
  141.  
  142.     
  143.     def test_sub(self):
  144.         i = self.s.difference(self.otherword)
  145.         self.assertEqual(self.s - set(self.otherword), i)
  146.         self.assertEqual(self.s - frozenset(self.otherword), i)
  147.         
  148.         try:
  149.             self.s - self.otherword
  150.         except TypeError:
  151.             pass
  152.  
  153.         self.fail('s-t did not screen-out general iterables')
  154.  
  155.     
  156.     def test_symmetric_difference(self):
  157.         i = self.s.symmetric_difference(self.otherword)
  158.         for c in self.letters:
  159.             self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword))
  160.         
  161.         self.assertEqual(self.s, self.thetype(self.word))
  162.         self.assertEqual(type(i), self.thetype)
  163.         self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru())
  164.         self.assertRaises(TypeError, self.s.symmetric_difference, [
  165.             []])
  166.         for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  167.             self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
  168.             self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
  169.             self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
  170.             self.assertEqual(self.thetype('abcba').symmetric_difference(C('ef')), set('abcef'))
  171.         
  172.  
  173.     
  174.     def test_xor(self):
  175.         i = self.s.symmetric_difference(self.otherword)
  176.         self.assertEqual(self.s ^ set(self.otherword), i)
  177.         self.assertEqual(self.s ^ frozenset(self.otherword), i)
  178.         
  179.         try:
  180.             self.s ^ self.otherword
  181.         except TypeError:
  182.             pass
  183.  
  184.         self.fail('s^t did not screen-out general iterables')
  185.  
  186.     
  187.     def test_equality(self):
  188.         self.assertEqual(self.s, set(self.word))
  189.         self.assertEqual(self.s, frozenset(self.word))
  190.         self.assertEqual(self.s == self.word, False)
  191.         self.assertNotEqual(self.s, set(self.otherword))
  192.         self.assertNotEqual(self.s, frozenset(self.otherword))
  193.         self.assertEqual(self.s != self.word, True)
  194.  
  195.     
  196.     def test_setOfFrozensets(self):
  197.         t = map(frozenset, [
  198.             'abcdef',
  199.             'bcd',
  200.             'bdcb',
  201.             'fed',
  202.             'fedccba'])
  203.         s = self.thetype(t)
  204.         self.assertEqual(len(s), 3)
  205.  
  206.     
  207.     def test_compare(self):
  208.         self.assertRaises(TypeError, self.s.__cmp__, self.s)
  209.  
  210.     
  211.     def test_sub_and_super(self):
  212.         (p, q, r) = map(self.thetype, [
  213.             'ab',
  214.             'abcde',
  215.             'def'])
  216.         self.assert_(p < q)
  217.         self.assert_(p <= q)
  218.         self.assert_(q <= q)
  219.         self.assert_(q > p)
  220.         self.assert_(q >= p)
  221.         self.failIf(q < r)
  222.         self.failIf(q <= r)
  223.         self.failIf(q > r)
  224.         self.failIf(q >= r)
  225.         self.assert_(set('a').issubset('abc'))
  226.         self.assert_(set('abc').issuperset('a'))
  227.         self.failIf(set('a').issubset('cbs'))
  228.         self.failIf(set('cbs').issuperset('a'))
  229.  
  230.     
  231.     def test_pickling(self):
  232.         for i in (0, 1, 2):
  233.             p = pickle.dumps(self.s, i)
  234.             dup = pickle.loads(p)
  235.             self.assertEqual(self.s, dup, '%s != %s' % (self.s, dup))
  236.             if type(self.s) not in (set, frozenset):
  237.                 self.s.x = 10
  238.                 p = pickle.dumps(self.s)
  239.                 dup = pickle.loads(p)
  240.                 self.assertEqual(self.s.x, dup.x)
  241.                 continue
  242.         
  243.  
  244.     
  245.     def test_deepcopy(self):
  246.         
  247.         class Tracer:
  248.             
  249.             def __init__(self, value):
  250.                 self.value = value
  251.  
  252.             
  253.             def __hash__(self):
  254.                 return self.value
  255.  
  256.             
  257.             def __deepcopy__(self, memo = None):
  258.                 return Tracer(self.value + 1)
  259.  
  260.  
  261.         t = Tracer(10)
  262.         s = self.thetype([
  263.             t])
  264.         dup = copy.deepcopy(s)
  265.         self.assertNotEqual(id(s), id(dup))
  266.         for elem in dup:
  267.             newt = elem
  268.         
  269.         self.assertNotEqual(id(t), id(newt))
  270.         self.assertEqual(t.value + 1, newt.value)
  271.  
  272.     
  273.     def test_gc(self):
  274.         
  275.         class A:
  276.             pass
  277.  
  278.         s = set((lambda [outmost-iterable]: for i in [outmost-iterable]:
  279. A())(xrange(1000)))
  280.         for elem in s:
  281.             elem.cycle = s
  282.             elem.sub = elem
  283.             elem.set = set([
  284.                 elem])
  285.         
  286.  
  287.     
  288.     def test_subclass_with_custom_hash(self):
  289.         
  290.         class H(self.thetype):
  291.             
  292.             def __hash__(self):
  293.                 return id(self)
  294.  
  295.  
  296.         s = H()
  297.         f = set()
  298.         f.add(s)
  299.         self.assert_(s in f)
  300.         f.remove(s)
  301.         f.add(s)
  302.         f.discard(s)
  303.  
  304.  
  305.  
  306. class TestSet(TestJointOps):
  307.     thetype = set
  308.     
  309.     def test_init(self):
  310.         s = self.thetype()
  311.         s.__init__(self.word)
  312.         self.assertEqual(s, set(self.word))
  313.         s.__init__(self.otherword)
  314.         self.assertEqual(s, set(self.otherword))
  315.         self.assertRaises(TypeError, s.__init__, s, 2)
  316.         self.assertRaises(TypeError, s.__init__, 1)
  317.  
  318.     
  319.     def test_constructor_identity(self):
  320.         s = self.thetype(range(3))
  321.         t = self.thetype(s)
  322.         self.assertNotEqual(id(s), id(t))
  323.  
  324.     
  325.     def test_hash(self):
  326.         self.assertRaises(TypeError, hash, self.s)
  327.  
  328.     
  329.     def test_clear(self):
  330.         self.s.clear()
  331.         self.assertEqual(self.s, set())
  332.         self.assertEqual(len(self.s), 0)
  333.  
  334.     
  335.     def test_copy(self):
  336.         dup = self.s.copy()
  337.         self.assertEqual(self.s, dup)
  338.         self.assertNotEqual(id(self.s), id(dup))
  339.  
  340.     
  341.     def test_add(self):
  342.         self.s.add('Q')
  343.         self.assert_('Q' in self.s)
  344.         dup = self.s.copy()
  345.         self.s.add('Q')
  346.         self.assertEqual(self.s, dup)
  347.         self.assertRaises(TypeError, self.s.add, [])
  348.  
  349.     
  350.     def test_remove(self):
  351.         self.s.remove('a')
  352.         self.assert_('a' not in self.s)
  353.         self.assertRaises(KeyError, self.s.remove, 'Q')
  354.         self.assertRaises(TypeError, self.s.remove, [])
  355.         s = self.thetype([
  356.             frozenset(self.word)])
  357.         self.assert_(self.thetype(self.word) in s)
  358.         s.remove(self.thetype(self.word))
  359.         self.assert_(self.thetype(self.word) not in s)
  360.         self.assertRaises(KeyError, self.s.remove, self.thetype(self.word))
  361.  
  362.     
  363.     def test_discard(self):
  364.         self.s.discard('a')
  365.         self.assert_('a' not in self.s)
  366.         self.s.discard('Q')
  367.         self.assertRaises(TypeError, self.s.discard, [])
  368.         s = self.thetype([
  369.             frozenset(self.word)])
  370.         self.assert_(self.thetype(self.word) in s)
  371.         s.discard(self.thetype(self.word))
  372.         self.assert_(self.thetype(self.word) not in s)
  373.         s.discard(self.thetype(self.word))
  374.  
  375.     
  376.     def test_pop(self):
  377.         for i in xrange(len(self.s)):
  378.             elem = self.s.pop()
  379.             self.assert_(elem not in self.s)
  380.         
  381.         self.assertRaises(KeyError, self.s.pop)
  382.  
  383.     
  384.     def test_update(self):
  385.         retval = self.s.update(self.otherword)
  386.         self.assertEqual(retval, None)
  387.         for c in self.word + self.otherword:
  388.             self.assert_(c in self.s)
  389.         
  390.         self.assertRaises(PassThru, self.s.update, check_pass_thru())
  391.         self.assertRaises(TypeError, self.s.update, [
  392.             []])
  393.         for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
  394.             for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  395.                 s = self.thetype('abcba')
  396.                 self.assertEqual(s.update(C(p)), None)
  397.                 self.assertEqual(s, set(q))
  398.             
  399.         
  400.  
  401.     
  402.     def test_ior(self):
  403.         self.s |= set(self.otherword)
  404.         for c in self.word + self.otherword:
  405.             self.assert_(c in self.s)
  406.         
  407.  
  408.     
  409.     def test_intersection_update(self):
  410.         retval = self.s.intersection_update(self.otherword)
  411.         self.assertEqual(retval, None)
  412.         for c in self.word + self.otherword:
  413.             if c in self.otherword and c in self.word:
  414.                 self.assert_(c in self.s)
  415.                 continue
  416.             self.assert_(c not in self.s)
  417.         
  418.         self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru())
  419.         self.assertRaises(TypeError, self.s.intersection_update, [
  420.             []])
  421.         for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')):
  422.             for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  423.                 s = self.thetype('abcba')
  424.                 self.assertEqual(s.intersection_update(C(p)), None)
  425.                 self.assertEqual(s, set(q))
  426.             
  427.         
  428.  
  429.     
  430.     def test_iand(self):
  431.         self.s &= set(self.otherword)
  432.         for c in self.word + self.otherword:
  433.             if c in self.otherword and c in self.word:
  434.                 self.assert_(c in self.s)
  435.                 continue
  436.             self
  437.             self.assert_(c not in self.s)
  438.         
  439.  
  440.     
  441.     def test_difference_update(self):
  442.         retval = self.s.difference_update(self.otherword)
  443.         self.assertEqual(retval, None)
  444.         for c in self.word + self.otherword:
  445.             if c in self.word and c not in self.otherword:
  446.                 self.assert_(c in self.s)
  447.                 continue
  448.             self.assert_(c not in self.s)
  449.         
  450.         self.assertRaises(PassThru, self.s.difference_update, check_pass_thru())
  451.         self.assertRaises(TypeError, self.s.difference_update, [
  452.             []])
  453.         self.assertRaises(TypeError, self.s.symmetric_difference_update, [
  454.             []])
  455.         for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')):
  456.             for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  457.                 s = self.thetype('abcba')
  458.                 self.assertEqual(s.difference_update(C(p)), None)
  459.                 self.assertEqual(s, set(q))
  460.             
  461.         
  462.  
  463.     
  464.     def test_isub(self):
  465.         self.s -= set(self.otherword)
  466.         for c in self.word + self.otherword:
  467.             if c in self.word and c not in self.otherword:
  468.                 self.assert_(c in self.s)
  469.                 continue
  470.             self
  471.             self.assert_(c not in self.s)
  472.         
  473.  
  474.     
  475.     def test_symmetric_difference_update(self):
  476.         retval = self.s.symmetric_difference_update(self.otherword)
  477.         self.assertEqual(retval, None)
  478.         for c in self.word + self.otherword:
  479.             if (c in self.word) ^ (c in self.otherword):
  480.                 self.assert_(c in self.s)
  481.                 continue
  482.             self.assert_(c not in self.s)
  483.         
  484.         self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru())
  485.         self.assertRaises(TypeError, self.s.symmetric_difference_update, [
  486.             []])
  487.         for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')):
  488.             for C in (set, frozenset, dict.fromkeys, str, unicode, list, tuple):
  489.                 s = self.thetype('abcba')
  490.                 self.assertEqual(s.symmetric_difference_update(C(p)), None)
  491.                 self.assertEqual(s, set(q))
  492.             
  493.         
  494.  
  495.     
  496.     def test_ixor(self):
  497.         self.s ^= set(self.otherword)
  498.         for c in self.word + self.otherword:
  499.             if (c in self.word) ^ (c in self.otherword):
  500.                 self.assert_(c in self.s)
  501.                 continue
  502.             self
  503.             self.assert_(c not in self.s)
  504.         
  505.  
  506.     
  507.     def test_inplace_on_self(self):
  508.         t = self.s.copy()
  509.         t |= t
  510.         self.assertEqual(t, self.s)
  511.         t &= t
  512.         self.assertEqual(t, self.s)
  513.         t -= t
  514.         self.assertEqual(t, self.thetype())
  515.         t = self.s.copy()
  516.         t ^= t
  517.         self.assertEqual(t, self.thetype())
  518.  
  519.     
  520.     def test_weakref(self):
  521.         s = self.thetype('gallahad')
  522.         p = proxy(s)
  523.         self.assertEqual(str(p), str(s))
  524.         s = None
  525.         self.assertRaises(ReferenceError, str, p)
  526.  
  527.  
  528.  
  529. class SetSubclass(set):
  530.     pass
  531.  
  532.  
  533. class TestSetSubclass(TestSet):
  534.     thetype = SetSubclass
  535.  
  536.  
  537. class TestFrozenSet(TestJointOps):
  538.     thetype = frozenset
  539.     
  540.     def test_init(self):
  541.         s = self.thetype(self.word)
  542.         s.__init__(self.otherword)
  543.         self.assertEqual(s, set(self.word))
  544.  
  545.     
  546.     def test_constructor_identity(self):
  547.         s = self.thetype(range(3))
  548.         t = self.thetype(s)
  549.         self.assertEqual(id(s), id(t))
  550.  
  551.     
  552.     def test_hash(self):
  553.         self.assertEqual(hash(self.thetype('abcdeb')), hash(self.thetype('ebecda')))
  554.  
  555.     
  556.     def test_copy(self):
  557.         dup = self.s.copy()
  558.         self.assertEqual(id(self.s), id(dup))
  559.  
  560.     
  561.     def test_frozen_as_dictkey(self):
  562.         seq = range(10) + list('abcdefg') + [
  563.             'apple']
  564.         key1 = self.thetype(seq)
  565.         key2 = self.thetype(reversed(seq))
  566.         self.assertEqual(key1, key2)
  567.         self.assertNotEqual(id(key1), id(key2))
  568.         d = { }
  569.         d[key1] = 42
  570.         self.assertEqual(d[key2], 42)
  571.  
  572.     
  573.     def test_hash_caching(self):
  574.         f = self.thetype('abcdcda')
  575.         self.assertEqual(hash(f), hash(f))
  576.  
  577.     
  578.     def test_hash_effectiveness(self):
  579.         n = 13
  580.         hashvalues = set()
  581.         addhashvalue = hashvalues.add
  582.         elemmasks = [ (i + 1, 1 << i) for i in range(n) ]
  583.         for i in xrange(2 ** n):
  584.             frozenset([]([](_[1])))
  585.         
  586.         self.assertEqual(len(hashvalues), 2 ** n)
  587.  
  588.  
  589.  
  590. class FrozenSetSubclass(frozenset):
  591.     pass
  592.  
  593.  
  594. class TestFrozenSetSubclass(TestFrozenSet):
  595.     thetype = FrozenSetSubclass
  596.     
  597.     def test_constructor_identity(self):
  598.         s = self.thetype(range(3))
  599.         t = self.thetype(s)
  600.         self.assertNotEqual(id(s), id(t))
  601.  
  602.     
  603.     def test_copy(self):
  604.         dup = self.s.copy()
  605.         self.assertNotEqual(id(self.s), id(dup))
  606.  
  607.     
  608.     def test_nested_empty_constructor(self):
  609.         s = self.thetype()
  610.         t = self.thetype(s)
  611.         self.assertEqual(s, t)
  612.  
  613.  
  614. empty_set = set()
  615.  
  616. class TestBasicOps(unittest.TestCase):
  617.     
  618.     def test_repr(self):
  619.         if self.repr is not None:
  620.             self.assertEqual(repr(self.set), self.repr)
  621.         
  622.  
  623.     
  624.     def test_print(self):
  625.         
  626.         try:
  627.             fo = open(test_support.TESTFN, 'wb')
  628.             print >>fo, self.set,
  629.             fo.close()
  630.             fo = open(test_support.TESTFN, 'rb')
  631.             self.assertEqual(fo.read(), repr(self.set))
  632.         finally:
  633.             fo.close()
  634.             os.remove(test_support.TESTFN)
  635.  
  636.  
  637.     
  638.     def test_length(self):
  639.         self.assertEqual(len(self.set), self.length)
  640.  
  641.     
  642.     def test_self_equality(self):
  643.         self.assertEqual(self.set, self.set)
  644.  
  645.     
  646.     def test_equivalent_equality(self):
  647.         self.assertEqual(self.set, self.dup)
  648.  
  649.     
  650.     def test_copy(self):
  651.         self.assertEqual(self.set.copy(), self.dup)
  652.  
  653.     
  654.     def test_self_union(self):
  655.         result = self.set | self.set
  656.         self.assertEqual(result, self.dup)
  657.  
  658.     
  659.     def test_empty_union(self):
  660.         result = self.set | empty_set
  661.         self.assertEqual(result, self.dup)
  662.  
  663.     
  664.     def test_union_empty(self):
  665.         result = empty_set | self.set
  666.         self.assertEqual(result, self.dup)
  667.  
  668.     
  669.     def test_self_intersection(self):
  670.         result = self.set & self.set
  671.         self.assertEqual(result, self.dup)
  672.  
  673.     
  674.     def test_empty_intersection(self):
  675.         result = self.set & empty_set
  676.         self.assertEqual(result, empty_set)
  677.  
  678.     
  679.     def test_intersection_empty(self):
  680.         result = empty_set & self.set
  681.         self.assertEqual(result, empty_set)
  682.  
  683.     
  684.     def test_self_symmetric_difference(self):
  685.         result = self.set ^ self.set
  686.         self.assertEqual(result, empty_set)
  687.  
  688.     
  689.     def checkempty_symmetric_difference(self):
  690.         result = self.set ^ empty_set
  691.         self.assertEqual(result, self.set)
  692.  
  693.     
  694.     def test_self_difference(self):
  695.         result = self.set - self.set
  696.         self.assertEqual(result, empty_set)
  697.  
  698.     
  699.     def test_empty_difference(self):
  700.         result = self.set - empty_set
  701.         self.assertEqual(result, self.dup)
  702.  
  703.     
  704.     def test_empty_difference_rev(self):
  705.         result = empty_set - self.set
  706.         self.assertEqual(result, empty_set)
  707.  
  708.     
  709.     def test_iteration(self):
  710.         for v in self.set:
  711.             self.assert_(v in self.values)
  712.         
  713.  
  714.     
  715.     def test_pickling(self):
  716.         p = pickle.dumps(self.set)
  717.         copy = pickle.loads(p)
  718.         self.assertEqual(self.set, copy, '%s != %s' % (self.set, copy))
  719.  
  720.  
  721.  
  722. class TestBasicOpsEmpty(TestBasicOps):
  723.     
  724.     def setUp(self):
  725.         self.case = 'empty set'
  726.         self.values = []
  727.         self.set = set(self.values)
  728.         self.dup = set(self.values)
  729.         self.length = 0
  730.         self.repr = 'set([])'
  731.  
  732.  
  733.  
  734. class TestBasicOpsSingleton(TestBasicOps):
  735.     
  736.     def setUp(self):
  737.         self.case = 'unit set (number)'
  738.         self.values = [
  739.             3]
  740.         self.set = set(self.values)
  741.         self.dup = set(self.values)
  742.         self.length = 1
  743.         self.repr = 'set([3])'
  744.  
  745.     
  746.     def test_in(self):
  747.         self.failUnless(3 in self.set)
  748.  
  749.     
  750.     def test_not_in(self):
  751.         self.failUnless(2 not in self.set)
  752.  
  753.  
  754.  
  755. class TestBasicOpsTuple(TestBasicOps):
  756.     
  757.     def setUp(self):
  758.         self.case = 'unit set (tuple)'
  759.         self.values = [
  760.             (0, 'zero')]
  761.         self.set = set(self.values)
  762.         self.dup = set(self.values)
  763.         self.length = 1
  764.         self.repr = "set([(0, 'zero')])"
  765.  
  766.     
  767.     def test_in(self):
  768.         self.failUnless((0, 'zero') in self.set)
  769.  
  770.     
  771.     def test_not_in(self):
  772.         self.failUnless(9 not in self.set)
  773.  
  774.  
  775.  
  776. class TestBasicOpsTriple(TestBasicOps):
  777.     
  778.     def setUp(self):
  779.         self.case = 'triple set'
  780.         self.values = [
  781.             0,
  782.             'zero',
  783.             operator.add]
  784.         self.set = set(self.values)
  785.         self.dup = set(self.values)
  786.         self.length = 3
  787.         self.repr = None
  788.  
  789.  
  790.  
  791. def baditer():
  792.     raise TypeError
  793.     yield True
  794.  
  795.  
  796. def gooditer():
  797.     yield True
  798.  
  799.  
  800. class TestExceptionPropagation(unittest.TestCase):
  801.     '''SF 628246:  Set constructor should not trap iterator TypeErrors'''
  802.     
  803.     def test_instanceWithException(self):
  804.         self.assertRaises(TypeError, set, baditer())
  805.  
  806.     
  807.     def test_instancesWithoutException(self):
  808.         set([
  809.             1,
  810.             2,
  811.             3])
  812.         set((1, 2, 3))
  813.         set({
  814.             'one': 1,
  815.             'two': 2,
  816.             'three': 3 })
  817.         set(xrange(3))
  818.         set('abc')
  819.         set(gooditer())
  820.  
  821.  
  822.  
  823. class TestSetOfSets(unittest.TestCase):
  824.     
  825.     def test_constructor(self):
  826.         inner = frozenset([
  827.             1])
  828.         outer = set([
  829.             inner])
  830.         element = outer.pop()
  831.         self.assertEqual(type(element), frozenset)
  832.         outer.add(inner)
  833.         outer.remove(inner)
  834.         self.assertEqual(outer, set())
  835.         outer.discard(inner)
  836.  
  837.  
  838.  
  839. class TestBinaryOps(unittest.TestCase):
  840.     
  841.     def setUp(self):
  842.         self.set = set((2, 4, 6))
  843.  
  844.     
  845.     def test_eq(self):
  846.         self.assertEqual(self.set, set({
  847.             2: 1,
  848.             4: 3,
  849.             6: 5 }))
  850.  
  851.     
  852.     def test_union_subset(self):
  853.         result = self.set | set([
  854.             2])
  855.         self.assertEqual(result, set((2, 4, 6)))
  856.  
  857.     
  858.     def test_union_superset(self):
  859.         result = self.set | set([
  860.             2,
  861.             4,
  862.             6,
  863.             8])
  864.         self.assertEqual(result, set([
  865.             2,
  866.             4,
  867.             6,
  868.             8]))
  869.  
  870.     
  871.     def test_union_overlap(self):
  872.         result = self.set | set([
  873.             3,
  874.             4,
  875.             5])
  876.         self.assertEqual(result, set([
  877.             2,
  878.             3,
  879.             4,
  880.             5,
  881.             6]))
  882.  
  883.     
  884.     def test_union_non_overlap(self):
  885.         result = self.set | set([
  886.             8])
  887.         self.assertEqual(result, set([
  888.             2,
  889.             4,
  890.             6,
  891.             8]))
  892.  
  893.     
  894.     def test_intersection_subset(self):
  895.         result = self.set & set((2, 4))
  896.         self.assertEqual(result, set((2, 4)))
  897.  
  898.     
  899.     def test_intersection_superset(self):
  900.         result = self.set & set([
  901.             2,
  902.             4,
  903.             6,
  904.             8])
  905.         self.assertEqual(result, set([
  906.             2,
  907.             4,
  908.             6]))
  909.  
  910.     
  911.     def test_intersection_overlap(self):
  912.         result = self.set & set([
  913.             3,
  914.             4,
  915.             5])
  916.         self.assertEqual(result, set([
  917.             4]))
  918.  
  919.     
  920.     def test_intersection_non_overlap(self):
  921.         result = self.set & set([
  922.             8])
  923.         self.assertEqual(result, empty_set)
  924.  
  925.     
  926.     def test_sym_difference_subset(self):
  927.         result = self.set ^ set((2, 4))
  928.         self.assertEqual(result, set([
  929.             6]))
  930.  
  931.     
  932.     def test_sym_difference_superset(self):
  933.         result = self.set ^ set((2, 4, 6, 8))
  934.         self.assertEqual(result, set([
  935.             8]))
  936.  
  937.     
  938.     def test_sym_difference_overlap(self):
  939.         result = self.set ^ set((3, 4, 5))
  940.         self.assertEqual(result, set([
  941.             2,
  942.             3,
  943.             5,
  944.             6]))
  945.  
  946.     
  947.     def test_sym_difference_non_overlap(self):
  948.         result = self.set ^ set([
  949.             8])
  950.         self.assertEqual(result, set([
  951.             2,
  952.             4,
  953.             6,
  954.             8]))
  955.  
  956.     
  957.     def test_cmp(self):
  958.         a = set('a')
  959.         b = set('b')
  960.         self.assertRaises(TypeError, cmp, a, b)
  961.         self.assertEqual(cmp(a, a), 0)
  962.         self.assertRaises(TypeError, cmp, a, 12)
  963.         self.assertRaises(TypeError, cmp, 'abc', a)
  964.  
  965.  
  966.  
  967. class TestUpdateOps(unittest.TestCase):
  968.     
  969.     def setUp(self):
  970.         self.set = set((2, 4, 6))
  971.  
  972.     
  973.     def test_union_subset(self):
  974.         self.set |= set([
  975.             2])
  976.         self.assertEqual(self.set, set((2, 4, 6)))
  977.  
  978.     
  979.     def test_union_superset(self):
  980.         self.set |= set([
  981.             2,
  982.             4,
  983.             6,
  984.             8])
  985.         self.assertEqual(self.set, set([
  986.             2,
  987.             4,
  988.             6,
  989.             8]))
  990.  
  991.     
  992.     def test_union_overlap(self):
  993.         self.set |= set([
  994.             3,
  995.             4,
  996.             5])
  997.         self.assertEqual(self.set, set([
  998.             2,
  999.             3,
  1000.             4,
  1001.             5,
  1002.             6]))
  1003.  
  1004.     
  1005.     def test_union_non_overlap(self):
  1006.         self.set |= set([
  1007.             8])
  1008.         self.assertEqual(self.set, set([
  1009.             2,
  1010.             4,
  1011.             6,
  1012.             8]))
  1013.  
  1014.     
  1015.     def test_union_method_call(self):
  1016.         self.set.update(set([
  1017.             3,
  1018.             4,
  1019.             5]))
  1020.         self.assertEqual(self.set, set([
  1021.             2,
  1022.             3,
  1023.             4,
  1024.             5,
  1025.             6]))
  1026.  
  1027.     
  1028.     def test_intersection_subset(self):
  1029.         self.set &= set((2, 4))
  1030.         self.assertEqual(self.set, set((2, 4)))
  1031.  
  1032.     
  1033.     def test_intersection_superset(self):
  1034.         self.set &= set([
  1035.             2,
  1036.             4,
  1037.             6,
  1038.             8])
  1039.         self.assertEqual(self.set, set([
  1040.             2,
  1041.             4,
  1042.             6]))
  1043.  
  1044.     
  1045.     def test_intersection_overlap(self):
  1046.         self.set &= set([
  1047.             3,
  1048.             4,
  1049.             5])
  1050.         self.assertEqual(self.set, set([
  1051.             4]))
  1052.  
  1053.     
  1054.     def test_intersection_non_overlap(self):
  1055.         self.set &= set([
  1056.             8])
  1057.         self.assertEqual(self.set, empty_set)
  1058.  
  1059.     
  1060.     def test_intersection_method_call(self):
  1061.         self.set.intersection_update(set([
  1062.             3,
  1063.             4,
  1064.             5]))
  1065.         self.assertEqual(self.set, set([
  1066.             4]))
  1067.  
  1068.     
  1069.     def test_sym_difference_subset(self):
  1070.         self.set ^= set((2, 4))
  1071.         self.assertEqual(self.set, set([
  1072.             6]))
  1073.  
  1074.     
  1075.     def test_sym_difference_superset(self):
  1076.         self.set ^= set((2, 4, 6, 8))
  1077.         self.assertEqual(self.set, set([
  1078.             8]))
  1079.  
  1080.     
  1081.     def test_sym_difference_overlap(self):
  1082.         self.set ^= set((3, 4, 5))
  1083.         self.assertEqual(self.set, set([
  1084.             2,
  1085.             3,
  1086.             5,
  1087.             6]))
  1088.  
  1089.     
  1090.     def test_sym_difference_non_overlap(self):
  1091.         self.set ^= set([
  1092.             8])
  1093.         self.assertEqual(self.set, set([
  1094.             2,
  1095.             4,
  1096.             6,
  1097.             8]))
  1098.  
  1099.     
  1100.     def test_sym_difference_method_call(self):
  1101.         self.set.symmetric_difference_update(set([
  1102.             3,
  1103.             4,
  1104.             5]))
  1105.         self.assertEqual(self.set, set([
  1106.             2,
  1107.             3,
  1108.             5,
  1109.             6]))
  1110.  
  1111.     
  1112.     def test_difference_subset(self):
  1113.         self.set -= set((2, 4))
  1114.         self.assertEqual(self.set, set([
  1115.             6]))
  1116.  
  1117.     
  1118.     def test_difference_superset(self):
  1119.         self.set -= set((2, 4, 6, 8))
  1120.         self.assertEqual(self.set, set([]))
  1121.  
  1122.     
  1123.     def test_difference_overlap(self):
  1124.         self.set -= set((3, 4, 5))
  1125.         self.assertEqual(self.set, set([
  1126.             2,
  1127.             6]))
  1128.  
  1129.     
  1130.     def test_difference_non_overlap(self):
  1131.         self.set -= set([
  1132.             8])
  1133.         self.assertEqual(self.set, set([
  1134.             2,
  1135.             4,
  1136.             6]))
  1137.  
  1138.     
  1139.     def test_difference_method_call(self):
  1140.         self.set.difference_update(set([
  1141.             3,
  1142.             4,
  1143.             5]))
  1144.         self.assertEqual(self.set, set([
  1145.             2,
  1146.             6]))
  1147.  
  1148.  
  1149.  
  1150. class TestMutate(unittest.TestCase):
  1151.     
  1152.     def setUp(self):
  1153.         self.values = [
  1154.             'a',
  1155.             'b',
  1156.             'c']
  1157.         self.set = set(self.values)
  1158.  
  1159.     
  1160.     def test_add_present(self):
  1161.         self.set.add('c')
  1162.         self.assertEqual(self.set, set('abc'))
  1163.  
  1164.     
  1165.     def test_add_absent(self):
  1166.         self.set.add('d')
  1167.         self.assertEqual(self.set, set('abcd'))
  1168.  
  1169.     
  1170.     def test_add_until_full(self):
  1171.         tmp = set()
  1172.         expected_len = 0
  1173.         for v in self.values:
  1174.             tmp.add(v)
  1175.             expected_len += 1
  1176.             self.assertEqual(len(tmp), expected_len)
  1177.         
  1178.         self.assertEqual(tmp, self.set)
  1179.  
  1180.     
  1181.     def test_remove_present(self):
  1182.         self.set.remove('b')
  1183.         self.assertEqual(self.set, set('ac'))
  1184.  
  1185.     
  1186.     def test_remove_absent(self):
  1187.         
  1188.         try:
  1189.             self.set.remove('d')
  1190.             self.fail('Removing missing element should have raised LookupError')
  1191.         except LookupError:
  1192.             pass
  1193.  
  1194.  
  1195.     
  1196.     def test_remove_until_empty(self):
  1197.         expected_len = len(self.set)
  1198.         for v in self.values:
  1199.             self.set.remove(v)
  1200.             expected_len -= 1
  1201.             self.assertEqual(len(self.set), expected_len)
  1202.         
  1203.  
  1204.     
  1205.     def test_discard_present(self):
  1206.         self.set.discard('c')
  1207.         self.assertEqual(self.set, set('ab'))
  1208.  
  1209.     
  1210.     def test_discard_absent(self):
  1211.         self.set.discard('d')
  1212.         self.assertEqual(self.set, set('abc'))
  1213.  
  1214.     
  1215.     def test_clear(self):
  1216.         self.set.clear()
  1217.         self.assertEqual(len(self.set), 0)
  1218.  
  1219.     
  1220.     def test_pop(self):
  1221.         popped = { }
  1222.         while self.set:
  1223.             popped[self.set.pop()] = None
  1224.         self.assertEqual(len(popped), len(self.values))
  1225.         for v in self.values:
  1226.             self.failUnless(v in popped)
  1227.         
  1228.  
  1229.     
  1230.     def test_update_empty_tuple(self):
  1231.         self.set.update(())
  1232.         self.assertEqual(self.set, set(self.values))
  1233.  
  1234.     
  1235.     def test_update_unit_tuple_overlap(self):
  1236.         self.set.update(('a',))
  1237.         self.assertEqual(self.set, set(self.values))
  1238.  
  1239.     
  1240.     def test_update_unit_tuple_non_overlap(self):
  1241.         self.set.update(('a', 'z'))
  1242.         self.assertEqual(self.set, set(self.values + [
  1243.             'z']))
  1244.  
  1245.  
  1246.  
  1247. class TestSubsets(unittest.TestCase):
  1248.     case2method = {
  1249.         '<=': 'issubset',
  1250.         '>=': 'issuperset' }
  1251.     reverse = {
  1252.         '==': '==',
  1253.         '!=': '!=',
  1254.         '<': '>',
  1255.         '>': '<',
  1256.         '<=': '>=',
  1257.         '>=': '<=' }
  1258.     
  1259.     def test_issubset(self):
  1260.         x = self.left
  1261.         y = self.right
  1262.         for case in ('!=', '==', '<', '<=', '>', '>='):
  1263.             expected = case in self.cases
  1264.             result = eval('x' + case + 'y', locals())
  1265.             self.assertEqual(result, expected)
  1266.             if case in TestSubsets.case2method:
  1267.                 method = getattr(x, TestSubsets.case2method[case])
  1268.                 result = method(y)
  1269.                 self.assertEqual(result, expected)
  1270.             
  1271.             rcase = TestSubsets.reverse[case]
  1272.             result = eval('y' + rcase + 'x', locals())
  1273.             self.assertEqual(result, expected)
  1274.             if rcase in TestSubsets.case2method:
  1275.                 method = getattr(y, TestSubsets.case2method[rcase])
  1276.                 result = method(x)
  1277.                 self.assertEqual(result, expected)
  1278.                 continue
  1279.         
  1280.  
  1281.  
  1282.  
  1283. class TestSubsetEqualEmpty(TestSubsets):
  1284.     left = set()
  1285.     right = set()
  1286.     name = 'both empty'
  1287.     cases = ('==', '<=', '>=')
  1288.  
  1289.  
  1290. class TestSubsetEqualNonEmpty(TestSubsets):
  1291.     left = set([
  1292.         1,
  1293.         2])
  1294.     right = set([
  1295.         1,
  1296.         2])
  1297.     name = 'equal pair'
  1298.     cases = ('==', '<=', '>=')
  1299.  
  1300.  
  1301. class TestSubsetEmptyNonEmpty(TestSubsets):
  1302.     left = set()
  1303.     right = set([
  1304.         1,
  1305.         2])
  1306.     name = 'one empty, one non-empty'
  1307.     cases = ('!=', '<', '<=')
  1308.  
  1309.  
  1310. class TestSubsetPartial(TestSubsets):
  1311.     left = set([
  1312.         1])
  1313.     right = set([
  1314.         1,
  1315.         2])
  1316.     name = 'one a non-empty proper subset of other'
  1317.     cases = ('!=', '<', '<=')
  1318.  
  1319.  
  1320. class TestSubsetNonOverlap(TestSubsets):
  1321.     left = set([
  1322.         1])
  1323.     right = set([
  1324.         2])
  1325.     name = 'neither empty, neither contains'
  1326.     cases = '!='
  1327.  
  1328.  
  1329. class TestOnlySetsInBinaryOps(unittest.TestCase):
  1330.     
  1331.     def test_eq_ne(self):
  1332.         self.assertEqual(self.other == self.set, False)
  1333.         self.assertEqual(self.set == self.other, False)
  1334.         self.assertEqual(self.other != self.set, True)
  1335.         self.assertEqual(self.set != self.other, True)
  1336.  
  1337.     
  1338.     def test_ge_gt_le_lt(self):
  1339.         self.assertRaises(TypeError, (lambda : self.set < self.other))
  1340.         self.assertRaises(TypeError, (lambda : self.set <= self.other))
  1341.         self.assertRaises(TypeError, (lambda : self.set > self.other))
  1342.         self.assertRaises(TypeError, (lambda : self.set >= self.other))
  1343.         self.assertRaises(TypeError, (lambda : self.other < self.set))
  1344.         self.assertRaises(TypeError, (lambda : self.other <= self.set))
  1345.         self.assertRaises(TypeError, (lambda : self.other > self.set))
  1346.         self.assertRaises(TypeError, (lambda : self.other >= self.set))
  1347.  
  1348.     
  1349.     def test_update_operator(self):
  1350.         
  1351.         try:
  1352.             self.set |= self.other
  1353.         except TypeError:
  1354.             pass
  1355.  
  1356.         self.fail('expected TypeError')
  1357.  
  1358.     
  1359.     def test_update(self):
  1360.         if self.otherIsIterable:
  1361.             self.set.update(self.other)
  1362.         else:
  1363.             self.assertRaises(TypeError, self.set.update, self.other)
  1364.  
  1365.     
  1366.     def test_union(self):
  1367.         self.assertRaises(TypeError, (lambda : self.set | self.other))
  1368.         self.assertRaises(TypeError, (lambda : self.other | self.set))
  1369.         if self.otherIsIterable:
  1370.             self.set.union(self.other)
  1371.         else:
  1372.             self.assertRaises(TypeError, self.set.union, self.other)
  1373.  
  1374.     
  1375.     def test_intersection_update_operator(self):
  1376.         
  1377.         try:
  1378.             self.set &= self.other
  1379.         except TypeError:
  1380.             pass
  1381.  
  1382.         self.fail('expected TypeError')
  1383.  
  1384.     
  1385.     def test_intersection_update(self):
  1386.         if self.otherIsIterable:
  1387.             self.set.intersection_update(self.other)
  1388.         else:
  1389.             self.assertRaises(TypeError, self.set.intersection_update, self.other)
  1390.  
  1391.     
  1392.     def test_intersection(self):
  1393.         self.assertRaises(TypeError, (lambda : self.set & self.other))
  1394.         self.assertRaises(TypeError, (lambda : self.other & self.set))
  1395.         if self.otherIsIterable:
  1396.             self.set.intersection(self.other)
  1397.         else:
  1398.             self.assertRaises(TypeError, self.set.intersection, self.other)
  1399.  
  1400.     
  1401.     def test_sym_difference_update_operator(self):
  1402.         
  1403.         try:
  1404.             self.set ^= self.other
  1405.         except TypeError:
  1406.             pass
  1407.  
  1408.         self.fail('expected TypeError')
  1409.  
  1410.     
  1411.     def test_sym_difference_update(self):
  1412.         if self.otherIsIterable:
  1413.             self.set.symmetric_difference_update(self.other)
  1414.         else:
  1415.             self.assertRaises(TypeError, self.set.symmetric_difference_update, self.other)
  1416.  
  1417.     
  1418.     def test_sym_difference(self):
  1419.         self.assertRaises(TypeError, (lambda : self.set ^ self.other))
  1420.         self.assertRaises(TypeError, (lambda : self.other ^ self.set))
  1421.         if self.otherIsIterable:
  1422.             self.set.symmetric_difference(self.other)
  1423.         else:
  1424.             self.assertRaises(TypeError, self.set.symmetric_difference, self.other)
  1425.  
  1426.     
  1427.     def test_difference_update_operator(self):
  1428.         
  1429.         try:
  1430.             self.set -= self.other
  1431.         except TypeError:
  1432.             pass
  1433.  
  1434.         self.fail('expected TypeError')
  1435.  
  1436.     
  1437.     def test_difference_update(self):
  1438.         if self.otherIsIterable:
  1439.             self.set.difference_update(self.other)
  1440.         else:
  1441.             self.assertRaises(TypeError, self.set.difference_update, self.other)
  1442.  
  1443.     
  1444.     def test_difference(self):
  1445.         self.assertRaises(TypeError, (lambda : self.set - self.other))
  1446.         self.assertRaises(TypeError, (lambda : self.other - self.set))
  1447.         if self.otherIsIterable:
  1448.             self.set.difference(self.other)
  1449.         else:
  1450.             self.assertRaises(TypeError, self.set.difference, self.other)
  1451.  
  1452.  
  1453.  
  1454. class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
  1455.     
  1456.     def setUp(self):
  1457.         self.set = set((1, 2, 3))
  1458.         self.other = 19
  1459.         self.otherIsIterable = False
  1460.  
  1461.  
  1462.  
  1463. class TestOnlySetsDict(TestOnlySetsInBinaryOps):
  1464.     
  1465.     def setUp(self):
  1466.         self.set = set((1, 2, 3))
  1467.         self.other = {
  1468.             1: 2,
  1469.             3: 4 }
  1470.         self.otherIsIterable = True
  1471.  
  1472.  
  1473.  
  1474. class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
  1475.     
  1476.     def setUp(self):
  1477.         self.set = set((1, 2, 3))
  1478.         self.other = operator.add
  1479.         self.otherIsIterable = False
  1480.  
  1481.  
  1482.  
  1483. class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
  1484.     
  1485.     def setUp(self):
  1486.         self.set = set((1, 2, 3))
  1487.         self.other = (2, 4, 6)
  1488.         self.otherIsIterable = True
  1489.  
  1490.  
  1491.  
  1492. class TestOnlySetsString(TestOnlySetsInBinaryOps):
  1493.     
  1494.     def setUp(self):
  1495.         self.set = set((1, 2, 3))
  1496.         self.other = 'abc'
  1497.         self.otherIsIterable = True
  1498.  
  1499.  
  1500.  
  1501. class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
  1502.     
  1503.     def setUp(self):
  1504.         
  1505.         def gen():
  1506.             for i in xrange(0, 10, 2):
  1507.                 yield i
  1508.             
  1509.  
  1510.         self.set = set((1, 2, 3))
  1511.         self.other = gen()
  1512.         self.otherIsIterable = True
  1513.  
  1514.  
  1515.  
  1516. class TestCopying(unittest.TestCase):
  1517.     
  1518.     def test_copy(self):
  1519.         dup = self.set.copy()
  1520.         dup_list = list(dup)
  1521.         dup_list.sort()
  1522.         set_list = list(self.set)
  1523.         set_list.sort()
  1524.         self.assertEqual(len(dup_list), len(set_list))
  1525.         for i in range(len(dup_list)):
  1526.             self.failUnless(dup_list[i] is set_list[i])
  1527.         
  1528.  
  1529.     
  1530.     def test_deep_copy(self):
  1531.         dup = copy.deepcopy(self.set)
  1532.         dup_list = list(dup)
  1533.         dup_list.sort()
  1534.         set_list = list(self.set)
  1535.         set_list.sort()
  1536.         self.assertEqual(len(dup_list), len(set_list))
  1537.         for i in range(len(dup_list)):
  1538.             self.assertEqual(dup_list[i], set_list[i])
  1539.         
  1540.  
  1541.  
  1542.  
  1543. class TestCopyingEmpty(TestCopying):
  1544.     
  1545.     def setUp(self):
  1546.         self.set = set()
  1547.  
  1548.  
  1549.  
  1550. class TestCopyingSingleton(TestCopying):
  1551.     
  1552.     def setUp(self):
  1553.         self.set = set([
  1554.             'hello'])
  1555.  
  1556.  
  1557.  
  1558. class TestCopyingTriple(TestCopying):
  1559.     
  1560.     def setUp(self):
  1561.         self.set = set([
  1562.             'zero',
  1563.             0,
  1564.             None])
  1565.  
  1566.  
  1567.  
  1568. class TestCopyingTuple(TestCopying):
  1569.     
  1570.     def setUp(self):
  1571.         self.set = set([
  1572.             (1, 2)])
  1573.  
  1574.  
  1575.  
  1576. class TestCopyingNested(TestCopying):
  1577.     
  1578.     def setUp(self):
  1579.         self.set = set([
  1580.             ((1, 2), (3, 4))])
  1581.  
  1582.  
  1583.  
  1584. class TestIdentities(unittest.TestCase):
  1585.     
  1586.     def setUp(self):
  1587.         self.a = set('abracadabra')
  1588.         self.b = set('alacazam')
  1589.  
  1590.     
  1591.     def test_binopsVsSubsets(self):
  1592.         a = self.a
  1593.         b = self.b
  1594.         self.assert_(a - b < a)
  1595.         self.assert_(b - a < b)
  1596.         self.assert_(a & b < a)
  1597.         self.assert_(a & b < b)
  1598.         self.assert_(a | b > a)
  1599.         self.assert_(a | b > b)
  1600.         self.assert_(a ^ b < a | b)
  1601.  
  1602.     
  1603.     def test_commutativity(self):
  1604.         a = self.a
  1605.         b = self.b
  1606.         self.assertEqual(a & b, b & a)
  1607.         self.assertEqual(a | b, b | a)
  1608.         self.assertEqual(a ^ b, b ^ a)
  1609.         if a != b:
  1610.             self.assertNotEqual(a - b, b - a)
  1611.         
  1612.  
  1613.     
  1614.     def test_summations(self):
  1615.         a = self.a
  1616.         b = self.b
  1617.         self.assertEqual(a - b | a & b | b - a, a | b)
  1618.         self.assertEqual(a & b | a ^ b, a | b)
  1619.         self.assertEqual(a | b - a, a | b)
  1620.         self.assertEqual(a - b | b, a | b)
  1621.         self.assertEqual(a - b | a & b, a)
  1622.         self.assertEqual(b - a | a & b, b)
  1623.         self.assertEqual(a - b | b - a, a ^ b)
  1624.  
  1625.     
  1626.     def test_exclusion(self):
  1627.         a = self.a
  1628.         b = self.b
  1629.         zero = set()
  1630.         self.assertEqual(a - b & b, zero)
  1631.         self.assertEqual(b - a & a, zero)
  1632.         self.assertEqual(a & b & (a ^ b), zero)
  1633.  
  1634.  
  1635.  
  1636. def R(seqn):
  1637.     '''Regular generator'''
  1638.     for i in seqn:
  1639.         yield i
  1640.     
  1641.  
  1642.  
  1643. class G:
  1644.     '''Sequence using __getitem__'''
  1645.     
  1646.     def __init__(self, seqn):
  1647.         self.seqn = seqn
  1648.  
  1649.     
  1650.     def __getitem__(self, i):
  1651.         return self.seqn[i]
  1652.  
  1653.  
  1654.  
  1655. class I:
  1656.     '''Sequence using iterator protocol'''
  1657.     
  1658.     def __init__(self, seqn):
  1659.         self.seqn = seqn
  1660.         self.i = 0
  1661.  
  1662.     
  1663.     def __iter__(self):
  1664.         return self
  1665.  
  1666.     
  1667.     def next(self):
  1668.         if self.i >= len(self.seqn):
  1669.             raise StopIteration
  1670.         
  1671.         v = self.seqn[self.i]
  1672.         self.i += 1
  1673.         return v
  1674.  
  1675.  
  1676.  
  1677. class Ig:
  1678.     '''Sequence using iterator protocol defined with a generator'''
  1679.     
  1680.     def __init__(self, seqn):
  1681.         self.seqn = seqn
  1682.         self.i = 0
  1683.  
  1684.     
  1685.     def __iter__(self):
  1686.         for val in self.seqn:
  1687.             yield val
  1688.         
  1689.  
  1690.  
  1691.  
  1692. class X:
  1693.     '''Missing __getitem__ and __iter__'''
  1694.     
  1695.     def __init__(self, seqn):
  1696.         self.seqn = seqn
  1697.         self.i = 0
  1698.  
  1699.     
  1700.     def next(self):
  1701.         if self.i >= len(self.seqn):
  1702.             raise StopIteration
  1703.         
  1704.         v = self.seqn[self.i]
  1705.         self.i += 1
  1706.         return v
  1707.  
  1708.  
  1709.  
  1710. class N:
  1711.     '''Iterator missing next()'''
  1712.     
  1713.     def __init__(self, seqn):
  1714.         self.seqn = seqn
  1715.         self.i = 0
  1716.  
  1717.     
  1718.     def __iter__(self):
  1719.         return self
  1720.  
  1721.  
  1722.  
  1723. class E:
  1724.     '''Test propagation of exceptions'''
  1725.     
  1726.     def __init__(self, seqn):
  1727.         self.seqn = seqn
  1728.         self.i = 0
  1729.  
  1730.     
  1731.     def __iter__(self):
  1732.         return self
  1733.  
  1734.     
  1735.     def next(self):
  1736.         3 // 0
  1737.  
  1738.  
  1739.  
  1740. class S:
  1741.     '''Test immediate stop'''
  1742.     
  1743.     def __init__(self, seqn):
  1744.         pass
  1745.  
  1746.     
  1747.     def __iter__(self):
  1748.         return self
  1749.  
  1750.     
  1751.     def next(self):
  1752.         raise StopIteration
  1753.  
  1754.  
  1755. from itertools import chain, imap
  1756.  
  1757. def L(seqn):
  1758.     '''Test multiple tiers of iterators'''
  1759.     return chain(imap((lambda x: x), R(Ig(G(seqn)))))
  1760.  
  1761.  
  1762. class TestVariousIteratorArgs(unittest.TestCase):
  1763.     
  1764.     def test_constructor(self):
  1765.         for cons in (set, frozenset):
  1766.             for s in ('123', '', range(1000), ('do', 1.2), xrange(2000, 2200, 5)):
  1767.                 for g in (G, I, Ig, S, L, R):
  1768.                     self.assertEqual(sorted(cons(g(s))), sorted(g(s)))
  1769.                 
  1770.                 self.assertRaises(TypeError, cons, X(s))
  1771.                 self.assertRaises(TypeError, cons, N(s))
  1772.                 self.assertRaises(ZeroDivisionError, cons, E(s))
  1773.             
  1774.         
  1775.  
  1776.     
  1777.     def test_inline_methods(self):
  1778.         s = set('november')
  1779.         for data in ('123', '', range(1000), ('do', 1.2), xrange(2000, 2200, 5), 'december'):
  1780.             for meth in (s.union, s.intersection, s.difference, s.symmetric_difference):
  1781.                 for g in (G, I, Ig, L, R):
  1782.                     expected = meth(data)
  1783.                     actual = meth(G(data))
  1784.                     self.assertEqual(sorted(actual), sorted(expected))
  1785.                 
  1786.                 self.assertRaises(TypeError, meth, X(s))
  1787.                 self.assertRaises(TypeError, meth, N(s))
  1788.                 self.assertRaises(ZeroDivisionError, meth, E(s))
  1789.             
  1790.         
  1791.  
  1792.     
  1793.     def test_inplace_methods(self):
  1794.         for data in ('123', '', range(1000), ('do', 1.2), xrange(2000, 2200, 5), 'december'):
  1795.             for methname in ('update', 'intersection_update', 'difference_update', 'symmetric_difference_update'):
  1796.                 for g in (G, I, Ig, S, L, R):
  1797.                     s = set('january')
  1798.                     t = s.copy()
  1799.                     getattr(s, methname)(list(g(data)))
  1800.                     getattr(t, methname)(g(data))
  1801.                     self.assertEqual(sorted(s), sorted(t))
  1802.                 
  1803.                 self.assertRaises(TypeError, getattr(set('january'), methname), X(data))
  1804.                 self.assertRaises(TypeError, getattr(set('january'), methname), N(data))
  1805.                 self.assertRaises(ZeroDivisionError, getattr(set('january'), methname), E(data))
  1806.             
  1807.         
  1808.  
  1809.  
  1810.  
  1811. def test_main(verbose = None):
  1812.     import sys as sys
  1813.     test_sets = test_sets
  1814.     import test
  1815.     test_classes = (TestSet, TestSetSubclass, TestFrozenSet, TestFrozenSetSubclass, TestSetOfSets, TestExceptionPropagation, TestBasicOpsEmpty, TestBasicOpsSingleton, TestBasicOpsTuple, TestBasicOpsTriple, TestBinaryOps, TestUpdateOps, TestMutate, TestSubsetEqualEmpty, TestSubsetEqualNonEmpty, TestSubsetEmptyNonEmpty, TestSubsetPartial, TestSubsetNonOverlap, TestOnlySetsNumeric, TestOnlySetsDict, TestOnlySetsOperator, TestOnlySetsTuple, TestOnlySetsString, TestOnlySetsGenerator, TestCopyingEmpty, TestCopyingSingleton, TestCopyingTriple, TestCopyingTuple, TestCopyingNested, TestIdentities, TestVariousIteratorArgs)
  1816.     test_support.run_unittest(*test_classes)
  1817.     if verbose and hasattr(sys, 'gettotalrefcount'):
  1818.         import gc as gc
  1819.         counts = [
  1820.             None] * 5
  1821.         for i in xrange(len(counts)):
  1822.             test_support.run_unittest(*test_classes)
  1823.             gc.collect()
  1824.             counts[i] = sys.gettotalrefcount()
  1825.         
  1826.         print counts
  1827.     
  1828.  
  1829. if __name__ == '__main__':
  1830.     test_main(verbose = True)
  1831.  
  1832.